﻿using System.Globalization;
using CommonEntity.CommonImageEntities;
using ImageDomain.Utils;
using itk.simple;

namespace ImageDomain.DE;
public class Image3d : IImage3d
{
    public bool IsMask { get; internal set; }
    public int OriginWindowMin { get; internal set; }
    public int OriginWindowMax { get; internal set; }
    public double[]? Spacing { get; internal set; }
    public double[]? Origin { get; internal set; }
    public double[]? Direction { get; internal set; }
    public uint[]? OriginSize { get; internal set; }
    public double ImageFactor { get; internal set; }

    public string SeriesDescription { get; internal set; }

    public Image? ItkImage { get; set; }

    public Dictionary<string, string> Tags { get; internal set; }

    public WindowAdjust WinAdjust { get; internal set; }

    /// <summary>
    /// 设置 dicom 序列
    /// </summary>
    /// <param name="files"></param>
    /// <returns></returns>
    public async Task SetImage(IList<string> files)
    {
        ItkImage = await ReadUtil.Instance.ReadImageAsync(files);
        Tags = await TagUtil.Instance.GetAllTagsAsync(files.FirstOrDefault()!);
        Initial();
    }

    /// <summary>
    /// 设置 mha mhd nii.gz 
    /// </summary>
    /// <param name="file"></param>
    /// <returns></returns>
    public async Task SetImage(string file)
    {
        ItkImage = await ReadUtil.Instance.ReadImageAsync(file);
        Tags = await TagUtil.Instance.GetAllTagsAsync(ItkImage);
        //SeriesDescription = new FileInfo(file).Name;
        Initial();
    }

    public void Initial()
    {
        if (ItkImage == null) return;

        var ctMinMax = ImageHandler.Instance.GetMinMaxVal(ItkImage);
        Origin = ItkImage.GetOrigin().ToArray();
        Direction = ItkImage.GetDirection().ToArray();
        Spacing = ItkImage.GetSpacing().ToArray();
        OriginSize = ItkImage.GetSize().ToArray();
        OriginWindowMin = ctMinMax[0];
        OriginWindowMax = ctMinMax[1];
        ImageFactor = Spacing[2] / Spacing[0];

        if (Tags != null && Tags.ContainsKey("0008|103E"))
        {
            SeriesDescription = Tags["0008|103E"];
        }
        else if(Tags != null && Tags.ContainsKey("0008|103e"))
        {
            SeriesDescription = Tags["0008|103e"];
        }

        if (string.IsNullOrEmpty(SeriesDescription))
        {
            SeriesDescription = $"Anonymous-{DateTime.Now.ToString("yyyyMMddHHmm", CultureInfo.InvariantCulture)}";
        }

        //center = ItkImage.TransformContinuousIndexToPhysicalPoint(new VectorDouble()
        //        { Origin[0] / 2, Origin[1] / 2, Origin[2] / 2 });

        var winLevel = (OriginWindowMax - OriginWindowMin) / 2;

        WinAdjust = new WindowAdjust()
        {
            OutputMax = 255,
            OutputMin = 0,
            WindowMin = OriginWindowMin + winLevel / 2,
            WindowMax = OriginWindowMax - winLevel / 2,
        };
    }

    /// <summary>
    /// 设置窗宽窗位
    /// </summary>
    /// <param name="start"></param>
    /// <param name="end"></param>
    public void SetWindowWidthCenter(int start, int end)
    {
        WinAdjust = new WindowAdjust()
        {
            OutputMax = 255,
            OutputMin = 0,
            WindowMin = start,
            WindowMax = end
        };
    }

    #region 资源释放
    private bool disposed = false;
    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }
    protected virtual void Dispose(bool disposing)
    {
        if (disposed) return;

        if (disposing)
        {
            // 释放托管资源
            ItkImage?.Dispose();
        }

        // 释放非托管资源

        disposed = true;
    }

    ~Image3d()
    {
        Dispose(false);
    }
    #endregion
}
